package cn.lena.weblog.controller;

import cn.lena.weblog.domain.Catagory;
import cn.lena.weblog.domain.Passage;
import cn.lena.weblog.domain.User;
import cn.lena.weblog.service.CatagoryService;
import cn.lena.weblog.service.PassageService;
import cn.lena.weblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/catagory")
public class CatagoryController {

	@Autowired
	private CatagoryService catagoryService;

	@Autowired
	private PassageService passageService;

	@Autowired
	private UserService userService;

	@GetMapping("/del/{id}")
	public String delCatagory(@PathVariable Integer id, HttpSession session, Model model) {
		Catagory c=new Catagory();
		Integer userId=((User)session.getAttribute("user")).getUserId();
		c.setUserId((userId));
		c.setCatagoryId(id);
		int i=catagoryService.delCatagory(c);
		//重新查询
		model.addAttribute("catagoryList",
				catagoryService.findByUserId(userId));
		if(i==1){
			model.addAttribute("Msg","删除成功");
		}else{
			model.addAttribute("Msg","删除失败");
		}
		return "user/catagory";
	}

	@GetMapping("/delCollect/{id}")
	public String delCollectCatagory(@PathVariable Integer id, HttpSession session, Model model) {
		Catagory c=new Catagory();
		Integer userId=((User)session.getAttribute("user")).getUserId();
		c.setUserId((userId));
		c.setCatagoryId(id);
		int i=catagoryService.delCatagory(c);
		//重新查询
		model.addAttribute("catagoryList",
				catagoryService.findCollectByUserId(userId));
		if(i==1){
			model.addAttribute("Msg","删除成功");
		}else{
			model.addAttribute("Msg","删除失败");
		}
		return "user/collect";
	}

	@GetMapping("/findCatagory/{id}")
	public String findCatagory(@PathVariable Integer id, HttpSession session, Model model) {
		if (session.getAttribute("user") == null) {
			model.addAttribute("login",false);
		}else{
			model.addAttribute("login",true);
		}
		//Integer userId=1001;
		Integer userId=(Integer)session.getAttribute("currentUser");
		User user = userService.findUserMore(userId);
		user.setRegistAge((int) Duration.between(LocalDateTime.now(),user.getJoinDate()).toDays()/365);
		model.addAttribute("author",user);

		List<Passage> passages = passageService.findCatagoryPassages(id);
		if(passages.size()==0){
			model.addAttribute("Msg","该分类下没有文章");
			return "catagory";
		}

		model.addAttribute("catagoryName",passages.get(0).getCatagoryName());
		model.addAttribute("passageList",passages);
		return "catagory";
	}


	@RequestMapping("/addCollect")		//t
	String addCollect(@RequestParam String catagoryName, HttpSession session, Model model){
		Catagory c=new Catagory();
		c.setCatagoryName(catagoryName).setIsCollect(1).setDeleted(0).setIsTop(0).setUserId(((User)session.getAttribute("user")).getUserId());
		int i = catagoryService.addCatagory(c);
		//重新查询
		model.addAttribute("catagoryList",
				catagoryService.findCollectByUserId(((User)session.getAttribute("user")).getUserId()));
		if(i==1){
			model.addAttribute("Msg","添加成功");
		}else{
			model.addAttribute("Msg","添加失败");
		}
		return "user/collect";
	}

	@RequestMapping("/findCollect")	//t
	String findCollect(HttpSession session, Model model){
		if(session.getAttribute("user")==null){
			return "user/login";	//用户没有登录，或者登录已经失效
		}
		model.addAttribute("catagoryList",
				catagoryService.findCollectByUserId(((User)session.getAttribute("user")).getUserId()));
		return "user/collect";
	}

	@RequestMapping("/add")//t
	String add(Catagory catagory, HttpSession session, Model model){
		if(session.getAttribute("user")==null){
			return "user/login";	//用户没有登录，或者登录已经失效
		}
		catagory.setUserId(((User)session.getAttribute("user")).getUserId());
		catagory.setIsTop(0);
		catagory.setDeleted(0);
		catagory.setIsCollect(0);
		int i = catagoryService.addCatagory(catagory);
		//重新查询
		model.addAttribute("catagoryList",
				catagoryService.findByUserId(((User)session.getAttribute("user")).getUserId()));
		if(i==1){
			model.addAttribute("Msg","添加成功");
		}else{
			model.addAttribute("Msg","添加失败");
		}
		return "user/catagory";
	}

	@RequestMapping("/findAll")//t
	List<Catagory> findAll(){
		return catagoryService.findAll();
	}

	@RequestMapping("/findByUser")	//t
	String findByUser(HttpSession session, Model model){
		if(session.getAttribute("user")==null){
			return "user/login";	//用户没有登录，或者登录已经失效
		}
		model.addAttribute("catagoryList",
				catagoryService.findByUserId(((User)session.getAttribute("user")).getUserId()));
		return "user/catagory";
	}

}
